<?php 
declare (strict_types = 1);
namespace app\api\controller;
use app\common\controller\Api;
use think\Request;
class BusinessMoeny extends Api
{
    //统计-财务统计
    public function index()
    {
        $this->businessCheckLogin();
        $post = param();
        if(!isset($post['first']) || !isset($post['last'])){
            return msgerror('参数缺失');
        }
        if($post['first'] > $post['last']) return msgerror('开始时间不能大于截止时间');
        if(!isset($post['type'])){
            //收支记录
            $where = [
                ['business_id','=',$this->businessinfo['id']],
                ['date','>=',$post['first']],
                ['date','<=',$post['last']]
            ];
        }elseif(isset($post['type']) && $post['type'] == 1){
            //只看收入
            $where = [
                ['business_id','=',$this->businessinfo['id']],
                ['date','>=',$post['first']],
                ['date','<=',$post['last']],
                ['status','=',1]
            ];
        }elseif(isset($post['type']) && $post['type'] == 2){
            //只看支出
            $where = [
                ['business_id','=',$this->businessinfo['id']],
                ['date','>=',$post['first']],
                ['date','<=',$post['last']],
                ['status','=',2]
            ];
        }else{
            return msgerror('type类型错误');
        }
        if(isset($post['business_money_type'])){
            $where[] = ['business_money_type_id','in',(string)$post['business_money_type']];
        }
        $data = $this->model->where($where)->with('business_money_type')->select();
        if(!empty($data) && !empty($data->toArray()))
        {
            $data = $data->toArray();
        }
        $first_time = date('Y-m-d',strtotime($post['first']));
        $last_time = date('Y-m-d',strtotime($post['last']));
        $day = (strtotime($last_time) - strtotime($first_time))/(60*60*24)+1;
        $day_data = [];
        for($i = 0;$i < $day;$i++){
            if($i == 0){
                $day_data[] = ['time'=>date('Y-m-d',strtotime($first_time))];
            }else{
                $day_data[] = ['time'=>date('Y-m-d',(strtotime($first_time)+(60*60*24*$i)))];
            }
        }
        foreach ($day_data as $k=>$v){
            $time = strtotime($v['time'])+(60*60*24)-1;
            if(isset($post['business_money_type'])){
                //收入
                $day_data[$k]['sr'] = $this->model->where([
                    ['business_id','=',$this->businessinfo['id']],
                    ['date','>=',$v['time']],
                    ['date','<=',date('Y-m-d H:i:s',$time)],
                    ['business_money_type_id','=',$post['business_money_type']],
                    ['status','=',1]
                ])->sum('money');
                //支出
                $day_data[$k]['zc'] = $this->model->where([
                    ['business_id','=',$this->businessinfo['id']],
                    ['date','>=',$v['time']],
                    ['date','<=',date('Y-m-d H:i:s',$time)],
                    ['business_money_type_id','=',$post['business_money_type']],
                    ['status','=',2]
                ])->sum('money');
            }else{
                //收入
                $day_data[$k]['sr'] = $this->model->where([
                    ['business_id','=',$this->businessinfo['id']],
                    ['date','>=',$v['time']],
                    ['date','<=',date('Y-m-d H:i:s',$time)],
                    ['status','=',1]
                ])->sum('money');
                //支出
                $day_data[$k]['zc'] = $this->model->where([
                    ['business_id','=',$this->businessinfo['id']],
                    ['date','>=',$v['time']],
                    ['date','<=',date('Y-m-d H:i:s',$time)],
                    ['status','=',2]
                ])->sum('money');
            }
            
            //结算
            $day_data[$k]['js'] = ($day_data[$k]['sr'] - $day_data[$k]['zc']);
        }
        $arr = [];
        $arr['data'] = $data;
        $arr['tj'] = $day_data;
        return datasuccess($arr);
    }
    //手动添加记录
    public function add()
    {
        $this->businessCheckLogin();
        $post = param();
        if(!isset($post['business_money_type_id']) || !isset($post['date']) || !isset($post['status']) || !isset($post['money'])) return msgerror('参数缺失');
        $arr = [
            'class_type'=>2,
            'business_id'=>$this->businessinfo['id'],
            'business_money_type_id'=>$post['business_money_type_id'],
            'date'=>$post['date'],
            'status'=>$post['status'],
            'money'=>$post['money'],
            'content'=>($post['content']??'')
        ];
        $this->model->save($arr);
        return datasuccess([],'添加成功');
    }
    //统计-流量统计
    public function ll_index()
    {
        $this->businessCheckLogin();
        $post = param();
        if(!isset($post['first']) || !isset($post['last'])){
            return msgerror('参数缺失');
        }
        if($post['first'] > $post['last']) return msgerror('开始时间不能大于截止时间');
        $data = [];
        $OrderFleet_model = app('app\api\model\OrderFleet');
        $where = [
            ['business_id','=',$this->businessinfo['id']],
            ['create_time','>=',$post['first']],
            ['create_time','<=',$post['last']]
        ];
        //时间段内的预约订单总数
        $data['order']['num'] = $OrderFleet_model->where($where)->count('id');
        $first_time = date('Y-m-d',strtotime($post['first']));
        $last_time = date('Y-m-d',strtotime($post['last']));
        $day = (strtotime($last_time) - strtotime($first_time))/(60*60*24)+1;
        $day_data = [];
        for($i = 0;$i < $day;$i++){
            if($i == 0){
                $day_data[] = ['time'=>date('Y-m-d',strtotime($first_time))];
            }else{
                $day_data[] = ['time'=>date('Y-m-d',(strtotime($first_time)+(60*60*24*$i)))];
            }
        }
        $member_data = $day_data;
        foreach ($day_data as $k=>$v){
            $time = strtotime($v['time'])+(60*60*24)-1;
            //各个日期预约订单数
            $day_data[$k]['num'] = $OrderFleet_model->where([
                ['business_id','=',$this->businessinfo['id']],
                ['create_time','>=',$v['time']],
                ['create_time','<=',date('Y-m-d H:i:s',$time)],
            ])->count('id');
        }
        $data['order']['data'] = $day_data;
        //预约人数
        $OrderMember_model = app('app\api\model\OrderMember');
        $where = [
            ['business_id','=',$this->businessinfo['id']],
            ['create_time','>=',$post['first']],
            ['create_time','<=',$post['last']],
            ['user_type','=',1],
        ];
        $data['member']['num'] = $OrderMember_model->where($where)->count('id');
        foreach ($member_data as $k=>$v){
            $time = strtotime($v['time'])+(60*60*24)-1;
            //各个日期预约订单数
            $member_data[$k]['num'] = $OrderMember_model->where([
                ['business_id','=',$this->businessinfo['id']],
                ['create_time','>=',$v['time']],
                ['create_time','<=',date('Y-m-d H:i:s',$time)],
                ['user_type','=',1],
            ])->count('id');
        }
        $data['member']['data'] = $member_data;
        return datasuccess($data);
    }
    //统计-剧本预约统计
    public function jb_index()
    {
        $this->businessCheckLogin();
        $post = param();
        $Script_model = app('app\api\model\Script');
        $order = input('order') ?: 'booking desc';
        $script_data = $Script_model->field('id,booking,name')->where('business_id','=',$this->businessinfo['id'])->order($order)->select();
        if(!empty($script_data) && !empty($script_data->toArray()))
        {
            $script_data = $script_data->toArray();
            foreach ($script_data as $k=>$v)
            {
                unset($script_data[$k]['booking']);
                $script_data[$k]['num'] = $v['booking'];
            }
        }
        $arr = [];
        if(isset($post['first']) && isset($post['last'])){
            if($post['first'] > $post['last']) return msgerror('开始时间不能大于截止时间');
            $ScriptYyLog_model = app('app\api\model\ScriptYyLog');
            foreach ($script_data as $k=>$v)
            {
                $count = $ScriptYyLog_model->where([
                    ['business_id','=',$this->businessinfo['id']],
                    ['script_id','=',$v['id']]
                ])->count('id');
                $arr[] = ['name'=>$v['name'],'num'=>$count];
            }
            $arr = arraySort($arr,'num',SORT_DESC);
        }else{
            $arr = $script_data;
        }
        return  datasuccess($arr);
    }
    //统计-剧本浏览量统计
    public function lll_index()
    {
        $this->businessCheckLogin();
        $post = param();
        $Script_model = app('app\api\model\Script');
        $order = input('order') ?: 'booking desc';
        $script_data = $Script_model->field('id,browse,name')->where('business_id','=',$this->businessinfo['id'])->order($order)->select();
        if(!empty($script_data) && !empty($script_data->toArray()))
        {
            $script_data = $script_data->toArray();
            foreach ($script_data as $k=>$v)
            {
                unset($script_data[$k]['booking']);
                $script_data[$k]['num'] = $v['browse'];
            }
        }
        $arr = [];
        if(isset($post['first']) && isset($post['last'])){
            if($post['first'] > $post['last']) return msgerror('开始时间不能大于截止时间');
            $ScriptYyLog_model = app('app\api\model\ScriptLlLog');
            foreach ($script_data as $k=>$v)
            {
                $count = $ScriptYyLog_model->where([
                    ['business_id','=',$this->businessinfo['id']],
                    ['script_id','=',$v['id']]
                ])->count('id');
                $arr[] = ['name'=>$v['name'],'num'=>$count];
            }
            $arr = arraySort($arr,'num',SORT_DESC);
        }else{
            $arr = $script_data;
        }
        return  datasuccess($arr);
    }
    //统计-流量统计-分享统计
    public function fx_index()
    {
        $this->businessCheckLogin();
        $post = param();
        $Share_model = app('app\admin\model\Share');
        if(!isset($post['first']) || !isset($post['last'])){
            return msgerror('参数缺失');
        }
        if($post['first'] > $post['last']) return msgerror('开始时间不能大于截止时间');
        $data = [];
        //合计次数
        $data['share']['num'] = $Share_model->where([
            ['business','=',$this->businessinfo['id']],
            ['create_time','>=',$post['first']],
            ['create_time','<=',$post['last']]
        ])->count('id');
        //合计人数
        $data['share']['member_num'] = $Share_model->where([
            ['business','=',$this->businessinfo['id']],
            ['create_time','>=',$post['first']],
            ['create_time','<=',$post['last']]
        ])->group('user_index_id')->count('id');
        //时间段内的
        $first_time = date('Y-m-d',strtotime($post['first']));
        $last_time = date('Y-m-d',strtotime($post['last']));
        $day = (strtotime($last_time) - strtotime($first_time))/(60*60*24)+1;
        $day_data = [];
        for($i = 0;$i < $day;$i++){
            if($i == 0){
                $day_data[] = ['time'=>date('Y-m-d',strtotime($first_time))];
            }else{
                $day_data[] = ['time'=>date('Y-m-d',(strtotime($first_time)+(60*60*24*$i)))];
            }
        }
        foreach ($day_data as $k=>$v){
            $time = strtotime($v['time'])+(60*60*24)-1;
            //各个日期次数
            $day_data[$k]['num'] = $Share_model->where([
                ['business','=',$this->businessinfo['id']],
                ['create_time','>=',$v['time']],
                ['create_time','<=',date('Y-m-d H:i:s',$time)],
            ])->count('id');
            //各个日期人数
            $day_data[$k]['member_num'] = $Share_model->where([
                ['business','=',$this->businessinfo['id']],
                ['create_time','>=',$v['time']],
                ['create_time','<=',date('Y-m-d H:i:s',$time)],
            ])->group('user_index_id')->count('id');
        }
        $data['data'] = $day_data;
        return  datasuccess($data);
    }
}